package single_game.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class RiderActions extends JPanel implements ActionListener, ChangeListener {

	private static final long serialVersionUID = 1L;
	public JButton maintain, relay;
	private RaceGui gui;
	public JSlider bar; 
	private Boolean canChange = true;
	
	public RiderActions(int width, int height, RaceGui gui) {
		
		this.gui = gui;	
		setSize(width, height);
		setLayout(null);
		
		maintain = new JButton("M");
		maintain.setBounds(0, 40, 50, 50);
		maintain.setOpaque(true);
		maintain.addActionListener(this);
		maintain.setBackground(Color.yellow);
		add(maintain);
		
		relay = new JButton("R");
		relay.setBounds(60, 40, 50, 50);
		relay.setOpaque(true);
		relay.addActionListener(this);
		relay.setBackground(Color.yellow);
		add(relay);
		
		bar = new JSlider(JSlider.HORIZONTAL, 0, 99, 50);
		bar.setMajorTickSpacing(10);
		bar.addChangeListener(this);
		bar.setBounds(0, 10, 200, 20);
		add(bar);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == maintain) {
			int actual = gui.lastSelected;
			if (actual != -1)
				gui.myTeam.get(actual).setAction("Maintain Position");
		}
		else if(e.getSource() == relay) {
			int actual = gui.lastSelected;
			if (actual != -1)
				gui.myTeam.get(actual).setAction("Relay Queue");
		}
	}
	
	public void setValue(int value) {
		canChange = false;
		bar.setValue(value);
		canChange = true;
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		int value = bar.getValue();
		int actual = gui.lastSelected;
		if (actual != -1 && canChange)
			gui.myTeam.get(actual).setPower(value);
		
	}
}
